/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TUILayer;

/**
 *
 * @author Martin
 */
import ModelLayer.*;
import ControlLayer.*;
import java.util.Scanner;

public class KunderUI {
    
private Scanner reader;
private AdressebogCtrl adressebogCtrl;
    
    public KunderUI(){
    reader = new Scanner(System.in);
    adressebogCtrl = new AdressebogCtrl();
    }
    
    /**
     * Calls a method in order to generate menu
     */
    public void start()
    {
        kunderMenu();
    }
    
    /**
     * Calls the address menu
     */
    public void kunderMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = udskrivKunderMenu(); 
          switch (choise)
          {   
              case 1: createPerson();
                      break;
              case 2: getPerson();
                      break;
              case 3: updatePerson();
                      break;
              case 4: deletePerson();
                      break;
              case 5: listAllPersons();
                      break;
              default: exit = true;
                      break;
          }
       }
    }
    
    /**
     * Generates the address menu
     */
    public int udskrivKunderMenu()
    {      // makes an object keyboard to read input from the screen
          
            System.out.println(" *** Kunder Menu ***");
            System.out.println(" (1) Opret Kunde");
            System.out.println(" (2) Find Kunde");
            System.out.println(" (3) Opdater Kundeinfo");
            System.out.println(" (4) Slet Kunde");
            System.out.println(" (5) Udskriv kunder");
            System.out.println(" (6) Hovedmenuen");
            System.out.print("\n Make your choice: ");
            
            int choise = reader.nextInt();
            //System.out.println("\f");
            return choise;
    }   
    /**
     * skaber et person objekt ved at sende oplysninger videre til adressebogCtrl
     */
    public void createPerson()
    {
          
        System.out.println("Navn : ");
        String navn = reader.nextLine();
        navn = reader.nextLine();
        reader.reset();
        System.out.println("Adresse : ");
        String adresse = reader.nextLine();
        //adresse = reader.nextLine();
        int tlf = inputTlf();
        System.out.println("Email : ");
        String email = reader.next();
        double kreditMax = inputKreditMax();
        adressebogCtrl.opretKunde(navn, adresse, tlf, email, kreditMax);
    }
    /**
     * henter et objekt af typen person ud fra tlf nummer
     */
    public void getPerson()
    {
        int tlf = inputTlf();
        System.out.println(adressebogCtrl.findPerson(tlf).toString());
        
    }
    /**
     * opdaterer et person objekt ved at sende oplysninger videre til adressebogCtrl
     */
    public void updatePerson()
    {
        int newTlf = inputNewTlf();
        System.out.println("Navn : ");
        String navn = reader.nextLine();
        navn = reader.nextLine();
        reader.reset();
        System.out.println("Adresse : ");
        String adresse = reader.nextLine();
        //adresse = reader.nextLine();
        int tlf = inputTlf();
        System.out.println("Email : ");
        String email = reader.next();
        adressebogCtrl.opdaterPerson(newTlf, navn, adresse, tlf, email);
    }
     /**
     * sletter et person objekt gennem adressebogCtrl via tlf nummer
     */
    public void deletePerson()
    {
        int tlf = inputTlf();
        adressebogCtrl.sletPerson(tlf);
    }
    /**
     * udskriver alle kunder i Person ArraylisteN
     */
    public void listAllPersons()
    {
        adressebogCtrl.udskrivKunder();
    }
    //----------------------------------------------------
    public int inputTlf()
    {   
        boolean ok = false;
        int tlf = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           
           System.out.println("Tlf :  ");
           try{
             tlf = reader.nextInt();
             ok = true;
           }
           catch (Exception e) // catch nullpointerexception i endnu en catch??
            {
             System.out.println("Der skal indtastes et tlf nummer -  prøv igen");
             String input = reader.nextLine();
           }
        }//end while
        return tlf;
    }
    //----------------------------------------------------
    public double inputKreditMax()
    {   
        boolean ok = false;
        double kreditMax = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           
           System.out.println("Kredit max :  ");
           try{
             kreditMax = reader.nextDouble();
             ok = true;
           }
           catch (Exception e) // catch nullpointerexception i endnu en catch??
            {
             System.out.println("Der skal indtastes et tal -  prøv igen");
             String input = reader.nextLine();
           }
        }//end while
        return kreditMax;
    }
    //-------------------------------------------------------------------
    public int inputNewTlf()
    {   
        boolean ok = false;
        int newTlf = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           
           System.out.println("Tlf :  ");
           try{
             newTlf = reader.nextInt();
             ok = true;
           }
           catch (Exception e) // catch nullpointerexception i endnu en catch??
            {
             System.out.println("Der skal indtastes et tlf nummer -  prøv igen");
             String input = reader.nextLine();
           }
        }//end while
        return newTlf;
    }
}
